Chris Pollett > Old Classes > CS174
( Print View )

Student Corner:
  [Submit Sec2]
  [Grades Sec2]

  [Submit Sec3]
  [Grades Sec3]

  [
Lecture Notes]
  [Discussion Board]

Course Info:
  [Texts & Links]
  [Description]
  [Course Outcomes]
  [Outcomes Matrix]
  [Course Schedule]
  [Grading]
  [Requirements/HW/Quizzes]
  [Class Protocols]
  [Exam Info]
  [Regrades]
  [University Policies]
  [Announcements]

HW Assignments:
  [Hw1]  [Hw2]  [Hw3]
  [Hw4]  [Hw5]  [Quizzes]

Practice Exams:
  [Midterm]  [Final]

                           












HW#2 --- last modified February 06 2019 04:09:27..

Solution set.

Due date: Oct 5

Files to be submitted:
  Hw1.zip

Purpose: To write a server-side application using PHP.

Related Course Outcomes:

The main course outcomes covered by this assignment are:

CLO1 -- Write HTML documents containing standard HTML elements including forms, tables, client-side scripts, and server-side scripts.

CLO3 -- Write server-side scripts that process HTML forms.

Specification:

Web analytics has been an important part of running a web-site since the days of web page counters in the 1990s. Two common approaches are to analyze server logs or to use page tagging. In log analytics, the web server log files are scanned and information about which IP addresses requests were made from, which pages were requested, the times of day, and which day, are extracted. In page tagging, an image or a script is added to web pages and whenever that script is requested by a browser, the download of the image or the execution of the script is recorded. Log scanning has the advantages that it works for all file types, not just html pages, and can also estimated web crawler tracker; page tracking has the advantages that you can get a third party to handle your analytics for you and you can better track human engagement with a page. This homework consists of two parts to help you learn about web analytics.

For the first part of the homework, I want you to locate the access logs for your installation of Apache. If access logging is turned off, I want you to turn it on. (If you are using MAMP rather than XAMPP, see How can I enable Apache Access logs.) Visit in a browser at least ten of page on your web site (you can visit default pages, phpmyadmin, pages you make up, etc.) Make sure at least one of the pages you visit has an image on it. Look at the log records in the access logs that are generated. In a file log_experiments.txt that you put in your Hw2.zip, write down a numeric list of the urls you visited in your browser. For example, you might have something like:

1 http://localhost/
2 http://localhost/Hw1/
3 http://localhost/phpmyadmin/
...
10 http://127.0.0.1/somewhere_site_number10/

Copy and paste the access log entries corresponding to these visits into log_experiments.txt. Beside each entry indicate which visit it corresponded to by writing the number of the visit. For example,

3  ::1 - - [19/Sep/2016:11:39:45 -0700] "GET /phpMyAdmin/themes/original/img/ajax_clock_small.gif HTTP/1.1" 200 1810

The '3' at the start of the above log file line indicate it was requested as a result of visiting the third website I originally listed, http://localhost/phpmyadmin/. Next I want you to look at the demo page of the awstats log analyzer site. For each chart rendered on the demo page, write a couple of sentences in your log_experiments.txt explaining how the information in that chart could be found by looking at log entries such as the above.

For the second part of the homework, I want to write a simple page tracking tool as well as page analytics viewer in PHP. You will submit this in a analytics subfolder of your Hw2 project zip. When placed in a folder of the grader's choice somewhere under Apaches' document root, and when navigated to by the grader in a browser, an HTML 5 validating landing page should be displayed by an index.php file. This landing page should have in a centered, h1 tag "Web Page Tagging Analytics". Styles for centering and making your site look cool should come from a file analytics.css. You should not use inline or document level styles. Beneath the h1 tag there should be a form with a text field, a select tag drop down and a button with Go on it. The method of the form should be get and it should be submitted to the default location (i.e., don't specify an action). The text field should have as placeholder, "Enter Site Magic String". A Site Magic String is any string that a user might want to use to identify their site. They can make it up, but they need to remember it so that they can use it to look up their site later. The drop down should have two values should be "Get Site Tracker Codes" and "View Analytics". It should default to View Analytics. Your index.php file should check if $_REQUEST['activity'] is set. If it is set, your script should check if it is one of the values: codes, counts, or analytics. If your script doesn't see $_REQUEST['activity'] set or it is not set to one of these values, then index.php should call a function landing() to draw the form described above. What to do if $_REQUEST['activity'] is set to one of these values we describe below.

If $_REQUEST['activity'] is set to codes, then the function codes() should be called. It is assumed that the user must have just entered a Site Magic String and that this was sent from the form in the variable arg, so if this variable is not set or it is empty, your program should call landing() and return. Otherwise, your program should check if the variable $_REQUEST['arg2'] is set and is not empty. If it is not set or is empty, the function codes() should draw a page with an h1 tag with text "Tracker Codes - Web Page Tagging Analytics". After this header, codes() should draw a form consisting of a textfield and a Go button. The textfield should have a placeholder attribute "Enter a URL to track" and name attribute arg2. If $_REQUEST['arg2'] is not empty, then one can assume this form has been submitted. In this case, your program calls PHP's sha1() function on the string obtained by concatenating $_REQUEST['arg'] . $_REQUEST['arg2']. Call the sha1 return value XXXX. Denote by YYYY the sha1 hash of $_REQUEST['arg']. Your script then outputs the same form but where the textfield now has value the URL just entered. Beneath the form, you should have an h2 tag with "Add the following code to the web page of the site with the url just entered". Then, using htmlentities to make this display properly, display the code snippet:

<script src="URL_TO_TRACKER_SITE/?activity=counts&arg=YYYY&arg2=XXXX" />

At the top of your index.php you should have a define("URL_TO_TRACKER_SITE" , "some_value"); statement where the grader can set URL_TO_TRACKER_SITE. XXXX and YYYY in the above are the returned values from the sha1 calls we mentioned earlier. After outputting, your program should read in the file url_lookups.txt, if it exists, and unserialize the contents to a variable $lookups. Then it should set $lookups[XXXX] = $_REQUEST['arg2']; serialize $lookups and write it back out to url_lookups.txt. If url_lookups.txt does not exist, then initialize $lookups = []; before setting $lookups[XXXX] = $_REQUEST['arg2']; and then serialize and write the file.

If $_REQUEST['activity'] is set to counts, then the function counts() should be called by your index.php script. This function just returns unless both $_REQUEST['arg'] and $_REQUEST['arg2'] are non-empty. If they are not empty, a file counts.txt, if it exists, should be read in and unserialized to the variable $counts. Otherwise, $counts should be set to []. Let IP be the value of $_SERVER['REMOTE_ADDR']. If $counts[$_REQUEST['arg']][$_REQUEST['arg2']][IP] is not set it should be initialized to 1, otherwise, it should be incremented. Then $counts should be serialized and the output written to the file counts.txt. Finally, your script should output the text:

tracking = "done";

If $_REQUEST['activity'] is set to analytics, then the function analytics() should be called by your index.php script. This function should output a page with an h1 tag banner, "View Analytics - Web Page Tagging Analytics". Beneath this banner should be an h2 tag "Analytics for MAGIC_STRING" where MAGIC_STRING is the value of $_REQUEST['arg']. Your program should read in and unserialize url_lookups.txt and counts.txt. For each array element XXXX in the $counts[$_REQUEST['arg']] it should look up YYYY in the unserialized url_lookups.txt to find what URL it corresponded to. It should output that url in an h3 tag followed by a total count of hits to that url. Beneath this it should have a table that lists for each IP address that requested that URL, the number of hits from that IP address.

This completes the description of what your program needs to do, below is the point breakdown.

Point Breakdown

Access log experiments in log_experiments.txt1pt
awstats write-up log_experiments.txt1pt
All web pages output by landing(), codes(), analytics() validate as HTML 5 (1/2pt), analytics.css used to style pages as described and can configure URL_TO_TRACKER_SITE as described. (1/2pt)1pt
Logic to decide which of landing(), counts(), codes(), analytics() is called works as described (1/2pt). Files counts.txt and url_lookups.txt created and updated as described (1/2pt)1pt
Each of the methods landing(), counts(), codes(), analytics() works as described (1 1/2pt each)6pts
Total10pts